<template>
  <div>
    <p>[ 98, 21, 34, 20, 12, 32, 56, 1, 4 ]</p>
    <p>{{ newArray }}</p>
  </div>
</template>

<script>
export default {
  name: "",
  data () {
    return {
      array: [98, 21, 34, 20, 12, 32, 56, 1, 4],
      newArray: []
    }
  },
  mounted () {
    this.newArray = this.bubbling(this.array)
  },
  methods: {
    bubbling (arr) {
      let a = arr
      let n = arr.length
      let condition
      do {
        condition = false
        for (let i = 0; i < n - 1; i++) {
          if (a[i] > a[i + 1]) {
            // 交换 a[i] 和 a[i + 1]
            let temp = a[i];
            a[i] = a[i + 1];
            a[i + 1] = temp;
            condition = true;
          }
        }
        n--
      } while (condition);
      return a
    }
  }

}
</script>

<style scoped>

</style>
